<?php

namespace app\lib\upload;

use app\lib\tool\Str;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class Qiniu
{
    public static function upload($file)
    {
        $accessKey = config('system.qiniu.accessKey');
        $secretKey = config('system.qiniu.secretKey');
        $bucket = config('system.qiniu.bucket');
        $auth = new Auth($accessKey, $secretKey);
        $token = $auth->uploadToken($bucket);

        $filePath = $file;
        $key = Str::random(1, 32);

        $uploadMgr = new UploadManager();
        list($res, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err == null) {
            return config('system.qiniu.domain') . '/' . $res['key'];
        } else {
            return false;
        }
    }
}
